package com.hangim.pce.service.impl;

import com.hangim.pce.common.service.impl.BaseEntityServiceImpl;
import com.hangim.pce.enums.RuleTargetType;
import com.hangim.pce.feign.UserService;
import com.hangim.pce.mapper.BaseElementMapper;
import com.hangim.pce.model.BaseElement;
import com.hangim.pce.model.SysDrugUsageRule;
import com.hangim.pce.model.SysElementRule;
import com.hangim.pce.model.vo.request.ElementVo;
import com.hangim.pce.service.BaseElementService;
import com.hangim.pce.service.SysDrugUsageRuleService;
import com.hangim.pce.service.SysElementRuleService;
import com.github.pig.common.util.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 药品表 服务实现类
 * </p>
 *
 * @author hongjiliang
 * @since 2018-10-19
 */
@Service
public class BaseElementServiceImpl extends BaseEntityServiceImpl<BaseElementMapper, BaseElement> implements BaseElementService {

    @Autowired
    private SysDrugUsageRuleService sysDrugUsageRuleService;

    @Autowired
    private SysElementRuleService sysElementRuleService;

    @Autowired
    private BaseElementService baseElementService;

    @Autowired
    private UserService userService;

    @Override
    public Boolean saveRules(ElementVo elementVo) {
        if (elementVo != null && elementVo.getElementId() != null) {
            //先删除所有规则
            SysElementRule elementRule = new SysElementRule();
            elementRule.setElementId(Long.parseLong(elementVo.getElementId()));
            sysElementRuleService.delete(elementRule);
            //保存成分规则
            if (elementVo.getElementRules() != null && elementVo.getElementRules().size() >= 1) {
                for (SysElementRule rule : elementVo.getElementRules()) {
                    rule.setElementId(Long.parseLong(elementVo.getElementId()));
                    rule.setElementName(elementVo.getElementName());
                    if (rule.getId() != null) {
                        rule.setCreatedAt(new Date());
                        rule.setCreatedBy(userService.findUserByUsername(UserUtils.getUser()).getUserId().longValue());
                    }
                    rule.setUpdatedAt(new Date());
                    rule.setUpdatedBy(userService.findUserByUsername(UserUtils.getUser()).getUserId().longValue());
                    rule.setActive(true);
                    sysElementRuleService.insert(rule);
                }
            }
            //先删除所有规则
            SysDrugUsageRule drugUsageRule = new SysDrugUsageRule();
            drugUsageRule.setDrugId(Long.parseLong(elementVo.getElementId()));
            sysDrugUsageRuleService.delete(drugUsageRule);
            //保存药品用法规则
            if (elementVo.getDrugUsageRules() != null && elementVo.getDrugUsageRules().size() >= 1) {
                for (SysDrugUsageRule rule : elementVo.getDrugUsageRules()) {
                    rule.setDrugId(Long.parseLong(elementVo.getElementId()));
                    rule.setType(RuleTargetType.ELEMENT.getNum());
                    if (rule.getId() != null) {
                        rule.setCreatedAt(new Date());
                        rule.setCreatedBy(userService.findUserByUsername(UserUtils.getUser()).getUserId().longValue());
                    }
                    //前端的数组转为字符串
                    if (rule.getUsages() != null && rule.getUsages().size() >= 1) {
                        StringBuffer builder = new StringBuffer();
                        for (Integer integer : rule.getUsages()) {
                            builder.append(integer);
                            builder.append(";");
                        }
                        rule.setUsage(builder.substring(0, builder.length() - 1));
                    }
                    rule.setUpdatedAt(new Date());
                    rule.setUpdatedBy(userService.findUserByUsername(UserUtils.getUser()).getUserId().longValue());
                    rule.setActive(true);
                    sysDrugUsageRuleService.insert(rule);
                }
            }

        }
        return true;
    }

    @Override
    public ElementVo getRules(Long id) {
        ElementVo vo = new ElementVo();
        BaseElement element = baseElementService.selectById(id);
        if (element != null) {
            vo.setElementId(String.valueOf(id));
            vo.setElementName(element.getName());
            vo.setElementRules(sysElementRuleService.getElementRulesByElementId(id));
            vo.setDrugUsageRules(sysDrugUsageRuleService.getDrugUsageRulesByDrugId(id));
        }
        return vo;
    }
}
